package team.you.change.util;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import team.you.change.dao.*;
import team.you.change.pojo.Table;

import javax.crypto.Cipher;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 随机生成课表
 */
@RunWith(SpringRunner.class)// SpringJUnit支持，由此引入Spring-Test框架支持！ 
@ContextConfiguration({"classpath*:mapper/*.xml","classpath:spring-mybatis.xml"})//用于加载bean
public class TableRandomCreate{
    //课程id列表
    private static List<String> COURSE;
    //教师id列表
    private static List<String> USER;
    //教室id列表
    private static List<String> ROOM;
    //班级id列表
    private static List<String> CLASS;

    private static final Logger LOGGER = LogManager.getRootLogger();
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private TableMapper tableMapper;
    @Test
    public void create() {
        getCourse(); // 获取相关信息
        getUser();
        getRoom();
        getClasses();
        Random random = new Random();
        for(int i = 0; i < 50; i++){
            Table table = new Table(); //构建数据
            table.setCourseId(COURSE.get(random.nextInt(COURSE.size())));
            table.setUserId(USER.get(random.nextInt(USER.size())));
            table.setRoomId(ROOM.get(random.nextInt(ROOM.size())));
            table.setClassId(CLASS.get(random.nextInt(CLASS.size())));
            Date date = getTime();
            table.setCreateTime(date);
            table.setSchoolYear("2020-2021");
            table.setSchoolTerm(2);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            table.setWeek(calendar.get(Calendar.WEEK_OF_YEAR) - 1);
            table.setWeekNum(calendar.get(Calendar.DAY_OF_WEEK) - 1);
            table.setTimeSlot(random.nextInt(5) + 1);
            table.setState(0);
            int insert = tableMapper.insert(table);
            if(insert > 0){
                LOGGER.info("成功");
            }else {
                LOGGER.info("失败");
            }
        }
    }

    /**
     * 验证是否重复 时间不冲突
     * @param table
     * @return
     */
    public boolean selectTable(Table table){

        return false;
    }

    /**
     * 获取一个随机日期
     * @return
     */
    private Date getTime(){
        Random random = new Random();
        int month = random.nextInt(4) + 4;
        int day = random.nextInt(31) + 1;
        String time = "2021/"+ month +"/"+day;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        try {
            Date date = sdf.parse(time);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取信息id列表
     */
    private void getCourse(){
        List<String> courseList = courseMapper.selectListId();
        if(courseList.size() > 0){
            COURSE = courseList;
        }else{
            System.out.println("获取课程id失败");
        }
    }
    private void getUser(){
        List<String> userList = userMapper.selectListId();
        if(userList.size() > 0){
            USER = userList;
        }else{
            System.out.println("获取教师id失败");
        }
    }
    private void getRoom(){
        List<String> roomList = roomMapper.selectListId();
        if(roomList.size() > 0){
            ROOM = roomList;
        }else{
            System.out.println("获取教室id失败");
        }
    }
    private void getClasses(){
        List<String> classList = classMapper.selectListId();
        if(classList.size() > 0){
            CLASS = classList;
        }else{
            System.out.println("获取班级id失败");
        }
    }
}
